package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.vo.NoticeListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/v1/notice")
@Api(tags = "通知模块")
public class NoticeController {
    @Autowired(required = false)
    private NoticeMapper noticeMapper;
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;
    @GetMapping("/list")
    @ApiOperation("查询通知列表")
    public JsonResult noticeList(){
        log.debug("开始处理查询通知列表的请求");
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        if (ops.get("noticeList")!=null){
            log.debug("从redis缓存中获取通知列表"+ops.get("noticeList"));
            return JsonResult.ok(ops.get("noticeList"));
        }
        List<NoticeListVO> noticeList = noticeMapper.selectNoticeList();

        ops.set("noticeList",(Serializable) noticeList,30, TimeUnit.SECONDS);
        log.debug("将通知列表放入redis缓存中,数据来源于mysql");
        return JsonResult.ok(noticeList);
    }

}
